package com.ruoyi.repair.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.repair.domain.CheMaintainImage;
import com.ruoyi.repair.domain.pojo.MaintainByShopId;
import com.ruoyi.repair.mapper.CheMaintainImageMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.repair.mapper.CheMaintainMapper;
import com.ruoyi.repair.domain.CheMaintain;
import com.ruoyi.repair.service.ICheMaintainService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 维修管理（其实用不到）Service业务层处理
 * 
 * @author 侯稼骏
 * @date 2024-12-31
 */
@Service
public class CheMaintainServiceImpl implements ICheMaintainService 
{
    @Autowired
    private CheMaintainMapper cheMaintainMapper;

    @Autowired
    private CheMaintainImageMapper cheMaintainImageMapper;

    @Override
    public MaintainByShopId selectByShopId(Long shopId) {
        // 初始化返回对象
        MaintainByShopId maintainByShopId = new MaintainByShopId();
        maintainByShopId.setShopId(shopId);

        // 查询所有维修记录
        List<CheMaintain> cheMaintains = cheMaintainMapper.selectallByShopId(shopId);
        if (cheMaintains == null || cheMaintains.isEmpty()) {
            maintainByShopId.setCheMaintains(Collections.emptyList());
            return maintainByShopId;
        }

        // 遍历维修记录并查询详细信息
        List<CheMaintain> detailedMaintains = new ArrayList<>();
        for (CheMaintain cheMaintain : cheMaintains) {
            if (cheMaintain != null && cheMaintain.getId() != null) {
                CheMaintain detailed = selectCheMaintainById(cheMaintain.getId());
                if (detailed != null) {
                    detailedMaintains.add(detailed);
                }
            }
        }

        maintainByShopId.setCheMaintains(detailedMaintains);
        return maintainByShopId;
    }

    /**
     * 查询维修管理（其实用不到）
     * 
     * @param id 维修管理（其实用不到）主键
     * @return 维修管理（其实用不到）
     */
    @Override
    public CheMaintain selectCheMaintainById(Long id) {
        try {
            CheMaintain cheMaintain = cheMaintainMapper.selectCheMaintainById(id);
            List<CheMaintainImage> cheMaintainImages = cheMaintainImageMapper.selectCheMaintainImageByMaintainId(id);
            if (!cheMaintainImages.isEmpty()) {
                cheMaintain.setCheMaintainImages(cheMaintainImages);
            }
            return cheMaintain;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("查询车辆维护信息及相关图片时出现异常", e);
        }
    }

    /**
     * 查询维修管理（其实用不到）列表
     * 
     * @param cheMaintain 维修管理（其实用不到）
     * @return 维修管理（其实用不到）
     */
    @Override
    public List<CheMaintain> selectCheMaintainList(CheMaintain cheMaintain)
    {
        System.out.println(cheMaintain);

        List<CheMaintain> cheMaintains = cheMaintainMapper.selectCheMaintainList(cheMaintain);
        return cheMaintains.stream()
                .peek(maintain -> {
                    Long id = maintain.getId();
                    List<CheMaintainImage> cheMaintainImages = cheMaintainImageMapper.selectCheMaintainImageByMaintainId(id);
                    if (!cheMaintainImages.isEmpty()) {
                        maintain.setCheMaintainImages(cheMaintainImages);
                    }
                })
                .collect(Collectors.toList());
    }

    /**
     * 新增维修管理（其实用不到）
     * 
     * @param cheMaintain 维修管理（其实用不到）
     * @return 结果
     */
    @Transactional
    @Override
    public Long insertCheMaintain(CheMaintain cheMaintain)
    {
        int i = cheMaintainMapper.insertCheMaintain(cheMaintain);

//        添加子表照片中间表数据
        String[] list = cheMaintain.getPhotoList();
        if (list != null){
            for (String s : list) {
                if(s == null || s.isEmpty()){
                    continue;
                }
                CheMaintainImage item = new CheMaintainImage();
                item.setMaintainId(cheMaintain.getId());
                item.setPhoto(s);
                cheMaintainImageMapper.insertCheMaintainImage(item);
            }
        }

//        List<CheMaintainImage> cheMaintainImages = cheMaintain.getCheMaintainImages();
//        if (cheMaintainImages != null && !cheMaintainImages.isEmpty()){
//            for (CheMaintainImage cheMaintainImage : cheMaintainImages) {
//                cheMaintainImageMapper.insertCheMaintainImage(cheMaintainImage);
//            }
//
//        }
        return cheMaintain.getId() == null ? 0 : cheMaintain.getId();
    }

    /**
     * 修改维修管理（其实用不到）
     * 
     * @param cheMaintain 维修管理（其实用不到）
     * @return 结果
     */
    @Override
    @Transactional
    public int updateCheMaintain(CheMaintain cheMaintain)
    {
        List<CheMaintainImage> cheMaintainImages = cheMaintain.getCheMaintainImages();
        if (cheMaintainImages != null &&  !cheMaintainImages.isEmpty()) {
            for (CheMaintainImage cheMaintainImage : cheMaintainImages) {
                        cheMaintainImageMapper.updateCheMaintainImage(cheMaintainImage);
            }
        }
        return cheMaintainMapper.updateCheMaintain(cheMaintain);
    }

    @Override
    public int updateCheMaintainState(CheMaintain cheMaintain) {
        Long id = cheMaintain.getId();
        CheMaintain cheMaintain1 = selectCheMaintainById(id);
        Long state = cheMaintain.getState();
        cheMaintain1.setState(state);
        return cheMaintainMapper.updateCheMaintain(cheMaintain1);
    }

    /**
     * 批量删除维修管理（其实用不到）
     * 
     * @param ids 需要删除的维修管理（其实用不到）主键
     * @return 结果
     */
    @Override
    public int deleteCheMaintainByIds(Long[] ids)
    {
        return cheMaintainMapper.deleteCheMaintainByIds(ids);
    }

    /**
     * 删除维修管理（其实用不到）信息
     * 
     * @param id 维修管理（其实用不到）主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteCheMaintainById(Long id) {
        List<CheMaintainImage> cheMaintainImages = cheMaintainImageMapper.selectCheMaintainImageByMaintainId(id);

        ArrayList<Long> longs = new ArrayList<>();
        for (CheMaintainImage cheMaintainImage : cheMaintainImages) {
            Long id1 = cheMaintainImage.getId();
            longs.add(id1);
        }

        Long[] ids = longs.toArray(new Long[longs.size()]);

        cheMaintainImageMapper.deleteCheMaintainImageByIds(ids);
        return cheMaintainMapper.deleteCheMaintainById(id);
    }
}
